package br.com.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;



import br.com.connection.Connection;
import br.com.faces.util.FacesUtil;
import br.com.model.Edital;
import br.com.model.Lance;
import br.com.model.Usuario;

public class EditalDao {

	public static boolean cadastrarEdital(Edital edital) {
		EntityManager manager = Connection.criarConexao();
		try {
			
			manager.getTransaction().begin();
			edital.setAtivo(1);
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			String mostra = sdf.format(edital.getDataInicio());
			String mostr = sdf.format(edital.getDataFim());
			System.out.println(mostra);
			System.out.println(mostr);
			manager.persist(edital);
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}

	public static ArrayList<Edital> listarEdital() {
		ArrayList<Edital> listaEdital= new ArrayList<Edital>();
		EntityManager manager = Connection.criarConexao();
		try {
			

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Edital> ce = cb.createQuery(Edital.class);
			Root<Edital> re = ce.from(Edital.class);
			ce.select(re);
			Predicate predicate = cb.equal(re.get("isAtivo"), 1);	
			ce.where(predicate);
			TypedQuery<Edital> query = manager.createQuery(ce);
			listaEdital = (ArrayList<Edital>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaEdital;
	}
	
	public static ArrayList<Edital> listarEditaisLanceUsuario(int idUsuario) {
		ArrayList<Edital> listaEdital= new ArrayList<Edital>();
		EntityManager manager = Connection.criarConexao();
		try {
			

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Edital> ce = cb.createQuery(Edital.class);
			Root<Edital> re = ce.from(Edital.class);
			ce.select(re);
			Predicate predicate = cb.equal(re.get("isAtivo"), 1);	
			ce.where(predicate);
			TypedQuery<Edital> query = manager.createQuery(ce);
			listaEdital = (ArrayList<Edital>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro: " + e);
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return listaEdital;
	}
	
	public static boolean removerEdital(Edital edital){
		EntityManager manager = Connection.criarConexao();
		try{
			
			manager.getTransaction().begin();
			edital.setAtivo(0);
			manager.merge(edital);
			manager.getTransaction().commit();
			manager.close();
		}catch(Exception ex){
			System.out.println("Erro: "+ex);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}
	
	public static boolean editarEdital(Edital edital){
		EntityManager manager = Connection.criarConexao();
		try{
			
			manager.getTransaction().begin();
			manager.merge(edital);
			manager.getTransaction().commit();
			manager.close();
		}catch(Exception ex){
			System.out.println("Erro: "+ex);
			return false;
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}
	
	public static Edital pesquPorId(int id){
		Edital edital = new Edital();
		EntityManager manager = Connection.criarConexao();
		try {
			
			edital = manager.find(Edital.class, id);
			manager.getTransaction().commit();
			manager.close();
			
		} catch (Exception e) {
			System.out.println("Erro"+e);
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
			return edital;
		}

}
